/*
 * Copyright！
 * 该文件由王福迎创建、编写、修改
 * 邮箱：<805447391@qq.com>
 * 版权所有！
 * 如有侵权，请联系邮箱
 * 转载请联系原作
 */

package com.sdjzu.wanfuying.androiddesign.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Author: nanchen
 * Email: liushilin520@foxmail.com
 * Date: 2017-06-20  14:47
 */

public abstract class BaseFragment extends Fragment {

    private Unbinder unbinder;
    private Handler mUIHandler = new Handler(Looper.getMainLooper());
    private Thread mUiThread = mUIHandler.getLooper().getThread();

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != unbinder)
            unbinder.unbind();
        unbinder = null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != mUIHandler)
            mUIHandler.removeCallbacksAndMessages(null);
        mUIHandler = null;
    }

    public void runOnUiThread(Runnable task, long duration) {
        if (task == null) return;
        mUIHandler.removeCallbacks(task);
        if ((duration > 0) || Thread.currentThread() != mUiThread) {
            mUIHandler.postDelayed(task, duration);
        } else {
            try {
                task.run();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void runOnUiThread(Runnable task) {
        Activity activity = getActivity();
        if (null != activity) {
            activity.runOnUiThread(task);
        }
    }

    public Handler getUIHandler() {
        return mUIHandler;
    }

    @NonNull
    @Override
    public Context getContext() {
        return super.getContext();
    }

    public static abstract class MyFragmentViewAdapter<T extends Fragment> extends PagerAdapter {
        @SuppressWarnings("unused")
        private static final String TAG = "FragmentPagerAdapter";
        @SuppressWarnings("unused")
        private static final boolean DEBUG = true;

        private final FragmentManager mFragmentManager;

        public MyFragmentViewAdapter(FragmentManager fm) {
            mFragmentManager = fm;
        }

        @SuppressWarnings("unused")
        private static String makeFragmentName(int viewId, int index) {
            return "android:switcher:" + viewId + ":" + index;
        }

        /**
         * Return the Fragment associated with a specified position.
         */
        public abstract T getItem(int position);

        @Override
        public void startUpdate(ViewGroup container) {
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Fragment fragment = getItem(position);
            if (!fragment.isAdded()) {
                FragmentTransaction ft = mFragmentManager.beginTransaction();
                ft.add(fragment, fragment.getClass().getName());
                ft.commit();
                mFragmentManager.executePendingTransactions();
            }
            if (fragment.getView() != null && fragment.getView().getParent() == null) {
                container.addView(fragment.getView()); // Ϊviewpager���Ӳ���
            }

            return fragment.getView();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }
    }
}


